package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;

/**
 * Created by zhangyanfei on 2019/7/4.
 *
 * @author zhangyanfei
 * @date 2019/07/04
 */
public class CountDownDemo {

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>(3);
        // 抛异常
        blockingQueue.add("a");
        blockingQueue.add("a");
        blockingQueue.add("a");

        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();

        //blockingQueue.element();
        //blockingQueue.element();
        //blockingQueue.element();

        // 返回true 和false
        blockingQueue.offer("a");
        blockingQueue.offer("a");
        blockingQueue.offer("a");
        System.out.println(blockingQueue.offer("a"));
        blockingQueue.poll();
        blockingQueue.poll();
        blockingQueue.poll();
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.peek());

        // 阻塞
        blockingQueue.put("a");
        blockingQueue.put("a");
        blockingQueue.put("a");
        blockingQueue.take();

        // 超时阻塞 offer(time)  poll(time)

    }

    private static void SemaphoreDemo() {
        // 信号灯 多个线程竞争多个资源 限流
        Semaphore semaphore = new Semaphore(4);

        for (int i = 0; i < 8; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + "come in ");
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphore.release();
                System.out.println(Thread.currentThread().getName() + "come out ");
            }, String.valueOf(i + "")).start();
        }
    }

    private static void CyclicBarrierDemo() {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("召唤神龙");
        });

        for (int i = 1; i <= 7; i++) {
            int finalI = i;
            new Thread(() -> {
                try {
                    System.out.println(finalI + "");
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }
    }

    private static void CountDownLatchDemo() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(7);

        for (int i = 0; i < 7; i++) {
            int finalI = i;
            new Thread(() -> {
                System.out.println(finalI + "");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println("main");
    }
}
